package com.csdj.t309tgspringboot.controller;

import com.csdj.t309tgspringboot.common.CacheContant;
import com.csdj.t309tgspringboot.common.WebResult;
import com.csdj.t309tgspringboot.entity.User;
import com.csdj.t309tgspringboot.service.IUserService;
import com.csdj.t309tgspringboot.service.RedisService;
import com.csdj.t309tgspringboot.util.Md5Utils;
import org.eclipse.jetty.util.ajax.JSON;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService iUserService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private User user;
    private User user1;
    private User user2;

    @ResponseBody
    @PostMapping("/login")
    public WebResult login(@RequestParam(value = "account",required = false) String account,
                           @RequestParam(value = "password",required = false) String password,
                           HttpServletResponse response,
                           HttpSession session){
        String pass = Md5Utils.md5(password);
        user = iUserService.login(account,pass);
        if(user!=null){
            session.setAttribute("uid",user.getId());
            int max=1000000000,min=1;
            int ran2 = (int) (Math.random()*(max-min)+min);
            String bd = Integer.toString(ran2)+Integer.toString(user.getId());
            stringRedisTemplate.opsForValue().set(bd,Integer.toString(ran2),30, TimeUnit.SECONDS);
            stringRedisTemplate.opsForValue().set("id",Integer.toString(user.getId()));
            /*stringRedisTemplate.opsForHash().hset();*/
            /*redisService.setObj(CacheContant.USER+user.getId(),JSON.toString(user),2);*/
            //创建Cookie对象
            Cookie c1=new Cookie("uid", Integer.toString(user.getId()));
            Cookie c2=new Cookie("token", Integer.toString(ran2));
            //设置Cookie的有效期(120分钟)
            c1.setMaxAge(120);
            c2.setMaxAge(120);
            response.addCookie(c1);
            response.addCookie(c2);
            WebResult webResult = new WebResult();
            webResult.putData("user",user);
            return webResult;
        }
        return null;
    }

    @ResponseBody
    @PostMapping("/registerUser")
    public WebResult registerUser(@RequestParam(value = "account",required = false) String account,
                                  @RequestParam(value = "password",required = false) String password,
                                  @RequestParam(value = "typeId",required = false) Integer typeId,
                                  @RequestParam(value = "name",required = false) String name,
                                  @RequestParam(value = "email",required = false) String email,
                                  @RequestParam(value = "condition",required = false) String condition){
        user = new User();
        user.setAccount(account);
        String pass = Md5Utils.md5(password);
        user.setPassword(pass);
        user.setTypeId(typeId);
        user.setName(name);
        user.setEmail(email);
        user.setCondition(condition);
        /*Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
        user.setCreatedDate(dateFormat.format(now));*/
        int num = iUserService.registerUser(user);
        if(num>0){
            WebResult webResult = new WebResult();
            webResult.putData("num",num);
            return webResult;
        }
        return null;
    }

    @ResponseBody
    @GetMapping("/seluser")
    public WebResult selUser(@RequestParam(value = "userId",defaultValue = "0") Integer userId){
        WebResult webResult = new WebResult();
        user = iUserService.selUsera(userId);
        if(user!=null){
            webResult.putData("user",user);
            return webResult;
        }
        return null;
    }
}
